home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / ImageButton.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  10.8 KB  |  408 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.awt.AWTException;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.Event;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.MediaTracker;
  11. import java.awt.image.FilteredImageSource;
  12. import java.awt.image.RGBImageFilter;
  13. import java.net.URL;
  14.  
  15. //    01/18/97    RKM    Changed setImageURL to handle getImage returning null, added call to invalidate
  16. //    01/29/97    TWB    Integrated changes from Macintosh
  17.  
  18. /* *
  19.  * The ImageButton component is similar to a regular button except that it 
  20.  * displays an image on the button's face. The image to use is specified with
  21.  * a URL.
  22.  * @version 1.0, Nov 26, 1996
  23.  * @author Symantec
  24.  */
  25.  
  26. /**
  27.  * <b>Description</b>
  28.  * <p>
  29.  * This component creates a rectangular button, with an image label, that 
  30.  * initiates an action. ImageButton extends the Button class.
  31.  * <p>
  32.  * Use an ImageButton to:
  33.  * <UL>
  34.  * <DT>╖ display an image instead of text on a button.</DT>
  35.  * <DT>╖ generate a train of action events when the user clicks the button.</DT>
  36.  * </UL>
  37.  * <b>Properties</b>
  38.  * <p>
  39.  * To select and display an image on the button, use the URL property. Specify 
  40.  * a file name for the JPEG or GIF image you want displayed. Then to scale the
  41.  * image to fit in the button, set the Scale Mode property.
  42.  * <p>
  43.  * To send a train of action events when the button is clicked, set the Notify
  44.  * While Pressed property to ôtrueö and specify a Notify Delay property value
  45.  * in milliseconds.
  46.  * <p>
  47.  * To create an interaction with another component, use the Interaction Wizard. 
  48.  * <p>
  49.  * @version 1.0, Nov 26, 1996
  50.  * @author Symantec
  51.  */
  52.  
  53. //    01/18/97    RKM    Changed setImageURL to handle getImage returning null, added call to invalidate
  54.  
  55. public class ImageButton
  56.     extends ButtonBase
  57. {
  58.     /**
  59.      * Scale image to fit button.
  60.      */
  61.     protected boolean scale;
  62.     private URL url;
  63.     private Image enabledImage;
  64.     private Image disabledImage;
  65.     private boolean centerMode;
  66.  
  67.     /**
  68.      * Construct a new default ImageButton. Image scaling off, center mode on.
  69.      */
  70.     public ImageButton()
  71.     {
  72.         scale           = false;
  73.         enabledImage    = null;
  74.         disabledImage   = null;
  75.         url             = null;
  76.         centerMode      = true;
  77.     }
  78.  
  79.     /**
  80.      * Sets the URL of the image to display on the button.
  81.      * @param u URL of image to display
  82.      * @see #getImageURL
  83.      */
  84.     public void setImageURL(URL u)
  85.         // throws AWTException
  86.     {
  87.         // Remove old images
  88.         
  89.         enabledImage = null;
  90.         if (disabledImage != null)
  91.             disabledImage.flush();
  92.         disabledImage = null;
  93.         
  94.         // Load new image
  95.         
  96.         url = u;
  97.         Image image = getToolkit().getImage(url);
  98.         if (image != null)
  99.         {
  100.             MediaTracker mt = new MediaTracker(this);
  101.             if (mt != null)
  102.             {
  103.                 try
  104.                 {
  105.                     mt.addImage(image, 0);
  106.                     mt.waitForAll();
  107.                 }
  108.                 catch (InterruptedException ie)
  109.                 {
  110.                 }
  111.                 
  112.                 if (mt.isErrorAny())
  113.                 {
  114.                     System.err.println("Error loading image " + image.toString());
  115.                     return;
  116.                 }
  117.                 
  118.                 enabledImage   = image;
  119.                 disabledImage  = createImage(
  120.                     new FilteredImageSource(image.getSource(),
  121.                             new ImageButtonDisableFilter()));
  122.                 
  123.                 //resize(image.getWidth(this) + bevel * 3 + 2, image.getHeight(this) + bevel * 3 + 2);
  124.             }
  125.         }
  126.         
  127.         invalidate();
  128.     }
  129.  
  130.     /**
  131.      * Returns the URL of the image being displayed on the button.
  132.      * @see #setImageURL
  133.      */
  134.     public URL getImageURL()
  135.     {
  136.         return url;
  137.     }
  138.  
  139.     /**
  140.      * Sets the scale mode of the button image.
  141.      * @param flag if true, the image is scaled to fit the button.  if false,
  142.      * the image is not scaled.
  143.      * @see #getScaleMode
  144.      */
  145.     public void setScaleMode(boolean flag)
  146.     {
  147.         if (scale != flag)
  148.         {
  149.             scale = flag;
  150.             invalidate();
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Returns the current scale mode of the button image.
  156.      * @see #setScaleMode
  157.      */
  158.     public boolean getScaleMode()
  159.     {
  160.         return scale;
  161.     }
  162.  
  163.     /**
  164.      * Sets the centering mode of the button image.
  165.      * @param flag if true, the image is centered on the button.  if false,
  166.      * the image is displayed starting in the upper-left corner.
  167.      * @see #getCenterMode
  168.      */
  169.     public void setCenterMode(boolean flag)
  170.     {
  171.         if (centerMode != flag)
  172.         {
  173.             centerMode = flag;
  174.             invalidate();
  175.         }
  176.     }
  177.  
  178.     /**
  179.      * Returns the current centering mode of the button image.
  180.      * @see #setCenterMode
  181.      */
  182.     public boolean getCenterMode()
  183.     {
  184.         return centerMode;
  185.     }
  186.  
  187.     /**
  188.      * Paints this component using the given graphics context.
  189.      * This is a standard Java AWT method which typically gets called
  190.      * by the AWT to handle painting this component. It paints this component
  191.      * using the given graphics context. The graphics context clipping region
  192.      * is set to the bounding rectangle of this component and its <0,0>
  193.      * coordinate is this component's top-left corner.
  194.      * 
  195.      * @param g the graphics context used for painting
  196.      * @see java.awt.Component#repaint
  197.      * @see #update
  198.      */
  199.     public void paint(Graphics g)
  200.     {
  201.         Image img = isEnabled() ? enabledImage : disabledImage;
  202.         
  203.         if (img == null)
  204.         {
  205.             super.paint(g);
  206.             return;
  207.         }
  208.         
  209.         paintHelper(g, img);
  210.         
  211.         Dimension s;
  212.         int x;
  213.         int y;
  214.         int w;
  215.         int h;
  216.         
  217.         s = size();
  218.         x = bevel + 1 + pressedAdjustment;
  219.         y = bevel + 1 + pressedAdjustment;
  220.         w = s.width  - 1;
  221.         h = s.height - 1;
  222.  
  223.        if (symantec.itools.lang.OS.isMacintosh()) 
  224.        {
  225.            if (scale)
  226.            {
  227.                 x = bevel + pressedAdjustment;
  228.                 y = bevel + pressedAdjustment;
  229.                 g.drawImage(img, x, y, w - bevel - bevel, h - bevel - bevel, this);
  230.            }
  231.            else
  232.            {
  233.                    if(centerMode)
  234.                 {
  235.                     x += (w - bevel - bevel - img.getWidth(this)) >> 1;
  236.                     y += (h - bevel - bevel - img.getHeight(this)) >> 1;
  237.                  }
  238.     
  239.                 g.drawImage(img, x, y, this);
  240.            }
  241.         }
  242.         else
  243.         {
  244.             if (centerMode && !scale)
  245.             {
  246.                 x += (w - img.getWidth(this)) / 2;
  247.                 y += (h - img.getHeight(this)) / 2;
  248.             }
  249.     
  250.             if(scale)
  251.             {
  252.                 g.drawImage(img, x, y, w - bevel * 2, h - bevel * 2, this);
  253.             }
  254.             else
  255.             {
  256.                 g.drawImage(img, x, y, this);
  257.             }
  258.         }
  259.     }
  260.  
  261.     /**
  262.      * Handles redrawing of this component on the screen.
  263.      * This is a standard Java AWT method which gets called by the Java
  264.      * AWT (repaint()) to handle repainting this component on the screen.
  265.      * The graphics context clipping region is set to the bounding rectangle
  266.      * of this component and its <0,0> coordinate is this component's 
  267.      * top-left corner.
  268.      * Typically this method paints the background color to clear the
  269.      * component's drawing space, sets graphics context to be the foreground
  270.      * color, and then calls paint() to draw the component.
  271.      * 
  272.      * It is overridden here to prevent the flicker associated with the standard 
  273.      * update() method's repainting of the background before painting the component
  274.      * itself.
  275.      *
  276.      * @param g the graphics context
  277.      * @see java.awt.Component#repaint
  278.      * @see #paint
  279.      */
  280.     public void update(Graphics g)
  281.     {
  282.         paint(g);
  283.     }
  284.     
  285.     private void paintHelper(Graphics g, Image img)
  286.     {
  287.         Dimension s;
  288.         int width;
  289.         int height;
  290.         int imgx;
  291.         int imgy;
  292.         int imgw;
  293.         int imgh;
  294.         int x;
  295.         int y;
  296.         int w;
  297.         int h;
  298.         int i;
  299.  
  300.         s        = size();
  301.         width    = s.width;
  302.         height    = s.height;
  303.         x        = bevel  + 1;
  304.         y        = bevel  + 1;
  305.         w        = width  - 1;
  306.         h        = height - 1;
  307.         imgx    = bevel + 1;
  308.         imgy    = bevel + 1;
  309.            imgw    = img.getWidth(this);
  310.         imgh    = img.getHeight(this);
  311.         
  312.         g.setColor(Color.lightGray);
  313.  
  314.         if (centerMode && !scale)
  315.         {
  316.             imgx += (width - 1 - bevel - bevel - imgw) >> 1;
  317.             imgy += (height - 1 - bevel - bevel - imgh) >> 1;
  318.         }
  319.                
  320.         if (pressed)
  321.         {
  322.             if(!scale)
  323.             {
  324.                  //Clean up artifacts
  325.                 g.fillRect(0, 0, imgx + bevel, height);
  326.                 g.fillRect(imgx + imgw + bevel, 0, width - imgx + imgw + bevel, height);
  327.                 g.fillRect(imgx + bevel, 0, imgw, imgy + bevel);
  328.                 g.fillRect(imgx + bevel, imgy + imgh + bevel, imgw, height - imgy + imgh + bevel);
  329.                 //End clean up
  330.                }
  331.             y = x += bevel > 0 ? 2 : 1;
  332.             g.setColor(Color.lightGray);
  333.  
  334.             for(i = 1; i < bevel + 1; i++)
  335.             {
  336.                 g.drawLine(i, h - i, w - i, h - i);
  337.                 g.drawLine(w - i, h - i, w - i, i);
  338.             }
  339.  
  340.             g.setColor(Color.gray);
  341.  
  342.             for(i = 1; i < bevel + 1; ++i)
  343.             {
  344.                 g.drawLine(i, h, i, i);
  345.                 g.drawLine(i, i, w, i);
  346.             }
  347.         }
  348.         else
  349.         {
  350.             if(!scale)
  351.             {
  352.                 //Clean up artifacts
  353.                 g.fillRect(0, 0, imgx, height);
  354.                 g.fillRect(imgx + imgw, 0, width - imgx + imgw, height);
  355.                 g.fillRect(imgx, 0, imgw, imgy);
  356.                 g.fillRect(imgx, imgy + imgh, imgw, height - imgy + imgh);
  357.                 //End clean up
  358.             }
  359.             
  360.             g.setColor(Color.white);
  361.  
  362.             for(i = 1; i < bevel + 1; i++)
  363.             {
  364.                 g.drawLine(i, h - i, i, i);
  365.                 g.drawLine(i, i, w - i, i);
  366.             }
  367.  
  368.             g.setColor(Color.gray);
  369.  
  370.             for(i = 1; i < bevel + 2; ++i)
  371.             {
  372.                 g.drawLine(i, h - i, w - i, h - i);
  373.                 g.drawLine(w - i, h - i, w - i, i);
  374.             }
  375.         }
  376.  
  377.         g.setColor(Color.black);
  378.         g.drawLine(1, 0, width - 2, 0);
  379.         g.drawLine(0, 1, 0, height - 2);
  380.         g.drawLine(1, height - 1, width - 2, height - 1);
  381.         g.drawLine(width - 1, height - 2, width - 1, 1);
  382.  
  383.         if(showInfoTip)
  384.         {
  385.             if (doInfoTip)
  386.             {
  387.                 drawInfoTip();
  388.             }
  389.         }
  390.     }
  391. }
  392.  
  393. class ImageButtonDisableFilter
  394.     extends RGBImageFilter
  395. {
  396.     public ImageButtonDisableFilter()
  397.     {
  398.         canFilterIndexColorModel = true;
  399.     }
  400.  
  401.  
  402.     public int filterRGB(int x, int y, int rgb)
  403.     {
  404.         return (rgb & ~0xff000000) | 0x80000000;
  405.     }
  406. }
  407.  
  408.